package com.llj.Road_curve_measuring_system.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.widget.EditText;
import android.widget.Toast;

import com.llj.Road_curve_measuring_system.R;
import com.llj.Road_curve_measuring_system.bean.ScreenResult;
import com.llj.Road_curve_measuring_system.bean.ypjResule;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnInputConfirmListener;
import com.tamsiree.rxui.view.dialog.RxDialogEditSureCancel;
import com.tamsiree.rxui.view.dialog.RxDialogShapeLoading;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：Dyj
 * @date ：Created in 2022/4/12 22:08
 * @description：汇聚各种其他功能
 * @modified By：
 * @version: 1.0
 */
public class MyUtil {

    /**
     * 全局context
     */
    private static Application mApplicationContext;


    /**
     * 加载显示的dialog
     */
    private static RxDialogShapeLoading rxDialogShapeLoading;


    /**
     * @param app 初始化全局context
     */
    public static void initialize(Application app) {
        mApplicationContext = app;
    }


    /**
     * 获得全局context
     *
     * @return 当前的全局context
     */
    public static Application getApplication() {
        return mApplicationContext;
    }



    /**
     * 展示dialog
     *
     * @param activity 传入当前Acitivity
     */
    public static void showLoading(Activity activity){
        rxDialogShapeLoading = new RxDialogShapeLoading(activity);
        rxDialogShapeLoading.setCanceledOnTouchOutside(false);
        rxDialogShapeLoading.setLoadingText(activity.getString(R.string.loading));
        rxDialogShapeLoading.show();
    }

    /**
     * 成功隐藏dialog，显示成功
     */
    public static void dismissSuccessLoading(){
        rxDialogShapeLoading.cancel(RxDialogShapeLoading.RxCancelType.success, getApplication().getString(R.string.calculation_success));
    }

    /**
     * 失败隐藏dialog，显示失败
     */
    public static void dismissFailedLoading(){
        rxDialogShapeLoading.cancel(RxDialogShapeLoading.RxCancelType.error, getApplication().getString(R.string.calculation_error));
    }

    /**
     * 将数据存入Sp中
     *
     * @param min
     * @param r
     * @param jd
     * @param interval
     * @param xxx
     * @param jdx
     * @param jdy
     * @param jdZy
     */
    public static void saveDataToSp(double min, double r, double jd, double interval, double xxx, double jdx, double jdy, double jdZy){
        SpUtil.setDouble("min",min);
        SpUtil.setDouble("r",r);
        SpUtil.setDouble("jd",jd);
        SpUtil.setDouble("interval",interval);
        SpUtil.setDouble("xxx",xxx);
        SpUtil.setDouble("jdx",jdx);
        SpUtil.setDouble("jdy",jdy);
        SpUtil.setDouble("jdZy",jdZy);
    }

    /**
     * 从sp中存数据到数据库
     *
     * @param path 路径
     * @param name 文件名
     */
    public static void saveDataToDataBase(int path, String name){
        ScreenResult screenResult = new ScreenResult();
        screenResult.setName(name);
        screenResult.setPath(path);
        screenResult.setMin(SpUtil.getDouble("min"));
        screenResult.setR(SpUtil.getDouble("r"));
        screenResult.setJd(SpUtil.getDouble("jd"));
        screenResult.setInterval(SpUtil.getDouble("interval"));
        screenResult.setXxx(SpUtil.getDouble("xxx"));
        screenResult.setJdx(SpUtil.getDouble("jdx"));
        screenResult.setJdy(SpUtil.getDouble("jdy"));
        screenResult.setJdZy(SpUtil.getDouble("jdZy"));
        screenResult.saveThrows();
    }

    /**
     * 调用带输入框的Dialog
     *
     * @param path 路径
     * @param activity 对应At的class
     */
    public static void editDialog(int path, Activity activity){

        new XPopup.Builder(activity).asInputConfirm(getApplication().getString(R.string.tx_title), getApplication().getString(R.string.tx_content),
                text -> {
                    MyUtil.saveDataToDataBase(path,text);
                    Toast.makeText(getApplication(),getApplication().getString(R.string.save_success),Toast.LENGTH_SHORT).show();
//                    ToastUtil.showToast(getApplication().getString(R.string.save_success));
                })
                .show();
    }


}
